import {HTTP_REQUEST_URL} from "@/config/app.js"
let base_url = HTTP_REQUEST_URL+'/'
// 带token
export const request = (params) => { //params 为我们需要调用的接口API的参数 
	let token = uni.getStorageSync('token')
	return new Promise(async (resolve, reject) => {
		const url = base_url + params.url; //请求的网络地址和局地的api地址组合
		return uni.request({
			url: url,
			data: params.data,
			method: params.method,
			header: {
				"Authorization": token,
				...params.header,
			},
			// header,
			success: async (res) => {
				if (res.data.code == 200) {
					resolve(res.data);
				} else {
					// 刷新token
					if (res.data.code == 4001) {
						if(uni.getStorageSync("isLoading")){
							return resolve(res.data)
						}
						uni.setStorageSync("isLoading",1)
						uni.showToast({
							title:"登录状态过期，需要重新登录",
							icon:"none",
						})
						setTimeout(()=>{
								uni.navigateTo({
									url:"/pages/login/login"
								})
						},1500)
						resolve(res.data)
					} else {
						uni.removeStorageSync('expriesAt')
						resolve(res.data);
					}
				}
			},
			fail: (err) => {
				uni.removeStorageSync('expriesAt')
				reject(err);
				console.log(err)
			},
			complete: () => {
				console.log('请求完成')
			}
		});
	})
}
